<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="isAll" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in $store.state.hero.goodList" :key="index">
          <th>
            <input
              type="checkbox"
              @click="heroChecked(index)"
              :checked="item.checked"
            />
          </th>
          <th>{{ item.name }}</th>
          <th>{{ item.price }}</th>
          <th>
            <button @click="heroNum(index, -1)">-</button> {{ item.num
            }}<button @click="heroNum(index, 1)">+</button>
          </th>

          <th>总价</th>
          <th><button @click="heroDel(index)">删除</button></th>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>英雄总数:</td>
          <td colspan="2">{{ $store.getters['hero/sum'] }}</td>
          <td>英雄总价:</td>
          <td colspan="2">{{ $store.getters['hero/sumprice'] }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  methods: {
    heroDel(val) {
      this.$store.commit('hero/heroDel', val)
    },
    heroChecked(val) {
      this.$store.commit('hero/heroChecked', val)
    },
    heroNum(index, val) {
      // console.log(index,val);
      this.$store.commit('hero/heroNum', { index, val })
    },
  },
  computed: {
    isAll: {
      get() {
        return this.$store.state.hero.goodList.every((item) => {
          return item.checked === true
        })
      },
      set(val) {
        this.$store.commit('hero/isALL', val)
      },
    },
  },
}
</script>

<style></style>
